"""
__project__ = 'thc'
__file_name__ = '数据库主体'
__author__ = 'thc'
__time__ = '2020/12/3/003 15:13'
__product_name = PyCharm
-*- coding:utf-8 -*-
"""

from pymysql import *
from 数据库信息 import db
import configparser


class DataBases(object):

    def __init__(self):
        config = configparser.ConfigParser()
        config.read(r'D:/自动化作业/le-bo16/thc/config.ini')
        self.host = config.get('db', 'db_host')
        self.port = eval(config.get('db', 'db_port'))
        self.user = config.get('db', 'db_user')
        self.password = config.get('db', 'db_password')
        self.database = config.get('db', 'db_name')

    # def __init__(self,
    #              host=db['host'],
    #              port=db['port'],
    #              user=db['user'],
    #              password=db['password'],
    #              database=db['data']):
    #     self.host = host
    #     self.port = port
    #     self.user = user
    #     self.password = password
    #     self.database = database

    def __git_connect(self):    # 链接数据库创建游标
        try:
            self.conn = connect(self.host, self.user, self.password, self.database, self.port)
            self.cur = self.conn.cursor()
        except Exception:
            raise RuntimeError('链接超时')
        return self.cur

    def git_select(self, sql, a=None):  # 查询
        self.__git_connect()
        try:
            self.cur.execute(sql)
            a = self.cur.fetchall()
            print('查询成功')
        except Exception:
            print('查询错误')
        return a

    def git_inserts(self, sql, a=None):  # 增加
        self.__git_connect()
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print('增加成功')
        except Exception:
            self.conn.rollback()
            print('增加失败')

    def git_updates(self, sql, a=None):  # 修改
        self.__git_connect()
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print('更改成功')
        except Exception:
            self.conn.rollback()
            print('更改失败')

    def git_deletes(self, sql, a=None):  # 删除
        self.__git_connect()
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print('删除成功')
        except Exception:
            self.conn.rollback()
            print('删除失败')


class XiTong(object):

    def __init__(self, host=db['host'],
                 port=db['port'],
                 user=db['user'],
                 password=db['password'],
                 database=db['data']):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.database = database

    def __git_connect(self):
        try:
            self.conn = connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database)
            self.cur = self.conn.cursor()
        except Exception:
            raise RuntimeError('链接超时')
        return self.cur

    def zhu_ye(self):
        while True:
            i = input('登陆输入1，注册输入2，退出输入3')
            if i == '1':
                self.__deng_lu()
            elif i == '2':
                self.__zhu_ce()
            elif i == '3':
                self.__tui_chu()
                break



    def __deng_lu(self):
        print('——————————现在开始登录——————————')
        dlzh = input('请输入账号')
        dlmm = input('请输入密码')
        self.__git_connect()
        try:
            sql1 = "select * from students where name = %s and studentNo = %s"
            sql2 = "select * from students where name = %s"
            a = self.cur.execute(sql1, [dlzh, dlmm])
            c = self.cur.fetchone()
            b = self.cur.execute(sql2, [dlzh])
        except Exception:
            print('当前网络不好')
        if a == 1 and b == 1:
            print('登录成功')
            print(c)
        elif b == 1:
            print('密码不正确，请重新输入')
        else:
            print('当前账号未注册，请前去注册')

    def __zhu_ce(self):
        print('——————————现在开始登录——————————')
        dlzh = input('请输入账号')
        dlmm = input('请输入密码')
        self.__git_connect()
        try:
            sql = 'select * from students where name = %s and studentNo = %s'
            a = self.cur.execute(sql, [dlzh, dlmm])
            if a == 1:
                print('当前用户已注册')
            else:
                sql = 'insert into students(studentNo, name) values(%s, %s)'
                self.cur.execute(sql, [dlmm, dlzh])
                self.conn.commit()
                print('注册成功')
        except Exception:
            self.conn.rollback()
            print('当前网络不好')

    def __tui_chu(self):
        self.__git_connect()
        self.cur.close()
        self.conn.close()
        print('退出成功')